package com.sita.friend.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.PopupMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.ui.activity.roadtrust.UserAroundActivity;
import com.sita.friend.ui.activity.FriendAddGroupAcitivity;
import com.sita.friend.ui.activity.FriendAddUserActivity;
import com.sita.friend.ui.activity.FriendMyActivity;
import com.sita.friend.ui.activity.FriendPublicGroupListActivity;
import com.sita.friend.utils.MessageUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class FriendFragment extends Fragment {

    private static final String TAG = FriendFragment.class.getSimpleName();
    @Bind(R.id.img_add_friend)
    ImageView imgAdd;
    @Bind(R.id.friend_rl_my_friends)
    public RelativeLayout rlFriendList;
    @Bind(R.id.friend_unread_msg_number)
    protected TextView tvUnreadFriendMessageNumber;
    @Bind(R.id.friend_rl_public_group)
    public RelativeLayout rlPublicGroupList;
    @Bind(R.id.friend_rl_person_around)
    public RelativeLayout rlPersonAround;

    private ConversationListFragment conversationListFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.friend_fragment_main, null);
        ButterKnife.bind(this, v);
        rlFriendList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FriendMyActivity.class));
            }
        });
        rlPublicGroupList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FriendPublicGroupListActivity.class));
            }
        });
        rlPersonAround.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), UserAroundActivity.class));
            }
        });
        tvUnreadFriendMessageNumber.setVisibility(View.INVISIBLE);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (conversationListFragment == null) {
            conversationListFragment = new ConversationListFragment();
            getActivity().getSupportFragmentManager().beginTransaction().add(R.id.chatlist_fragment_container, conversationListFragment)
                    .show(conversationListFragment)
                    .commit();
        }
        refreshInviteMessage();
    }

    @OnClick(R.id.img_add_friend)
    void handleAddFriend() {
        PopupMenu popup = new PopupMenu(getActivity(), imgAdd);
        //Inflating the Popup using xml file
        popup.getMenuInflater()
                .inflate(R.menu.friend_add_menu, popup.getMenu());

        try {
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popup);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper
                            .getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //registering popup with OnMenuItemClickListener
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.add_friend) {
                    // search person
//                    startActivity(new Intent(getActivity(), FriendMyActivity.class));
                    startActivity(new Intent(getActivity(), FriendAddUserActivity.class));
                    return true;
                } else if (item.getItemId() == R.id.friend_add_team) {
                    // create group
                    startActivity(new Intent(getActivity(), FriendAddGroupAcitivity.class));
                    return true;
                } else {
                    return true;
                }
            }
        });
        popup.show();//showing popup menu 
    }

    public void refreshInviteMessage() {
        if (tvUnreadFriendMessageNumber == null) {
            return;
        }
        // new friend message;
        int count = MessageUtils.getUnreadInviteMessageCount();
        if (count > 0) {
            tvUnreadFriendMessageNumber.setText(String.valueOf(count));
            tvUnreadFriendMessageNumber.setVisibility(View.VISIBLE);
        } else {
            tvUnreadFriendMessageNumber.setText(String.valueOf(count));
            tvUnreadFriendMessageNumber.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 刷新页面
     */
    public void refresh() {
        refreshInviteMessage();
        // new message;
        if (conversationListFragment != null) {
            conversationListFragment.refresh();
        }
    }
}
